package com.zzty.txj.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class Map1 {
    public static final String ak="KQqexg4QpRIK1gg6xIrjpD5KZ7GoYY0v";

    public static void main(String[] args) throws Exception{

        getLatAndLngByAddress("上海市浦东新区环湖西二路800号");
//        System.out.println("get address");
//        String address="上海市浦东新区环湖西二路800号";
//        String accessToken="";
//        String url="http://api.map.baidu.com/geocoder/v2/?address={address}&output=json&ak={ak}&callback=showLocation";
//        url.replace("{ak}",ak);
//        url.replace("{address}",address);
//        //url=URLEncoder.encode(url, "utf-8");
//        URL url2=new URL(url);
//        HttpsURLConnection con=(HttpsURLConnection) url2.openConnection();
//        con.setRequestMethod("GET");
//        con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//        con.setDoOutput(true);
//        con.setDoInput(true);
//
//        con.connect();
//        InputStream input=con.getInputStream();
//        byte[] c=new byte[input.available()];
//        input.read(c);
//        // String有一个构造方法，可以接受一个，字节的数组，来创建一个String.
//        //这个过程，就实现了，字节数组转化为String的过程。
//        String message=new String(c,"UTF-8");
//        input.close();
//        con.disconnect();
//
//        JSONObject demoJson;
//        System.out.println("message  "+message);
//        demoJson = JSONObject.fromObject(message);
//        accessToken=demoJson.getString("access_token");



    }

    public static Map<String, BigDecimal> getLatAndLngByAddress(String addr){
        String address = "";
        String lat = "";
        String lng = "";
        try {
            address = URLEncoder.encode(addr,"UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        String url = String.format("http://api.map.baidu.com/geocoder/v2/?"
                +"ak=KQqexg4QpRIK1gg6xIrjpD5KZ7GoYY0v&output=json&address=%s",address);
        URL myURL = null;
        URLConnection httpsConn = null;
        //进行转码
        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {

        }
        try {
            httpsConn = (URLConnection) myURL.openConnection();
            if (httpsConn != null) {
                InputStreamReader insr = new InputStreamReader(
                        httpsConn.getInputStream(), "UTF-8");
                BufferedReader br = new BufferedReader(insr);
                String data = null;
                if ((data = br.readLine()) != null) {
                    lat = data.substring(data.indexOf("\"lat\":")
                            + ("\"lat\":").length(), data.indexOf("},\"precise\""));
                    lng = data.substring(data.indexOf("\"lng\":")
                            + ("\"lng\":").length(), data.indexOf(",\"lat\""));
                }
                insr.close();
            }
        } catch (IOException e) {

        }
        Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
        map.put("lat", new BigDecimal(lat));
        map.put("lng", new BigDecimal(lng));
        return map;
    }

}
